///<reference path="references.js" />
if ( !window.Gorbatras ) window.Gorbatras = {};
if ( !window.Gorbatras.Admin ) window.Gorbatras.Admin = {};

$.getScript( "../js/textureSelection.js", function () { Gorbatras.Admin.TextureSelection.Init() } );
$.getScript( "../js/filecontrol.js" );


Gorbatras.Admin.ImagePopup = function ( a_text, a_src, a_clientHeight, a_clientWidth, a_documentHeight, a_documentWidth, a_scrolledTop )
{
    $( "body" )
    .append(
        $( '<div id="popup_wrap">' )
        //The click function to remove the popup
        .click( function ()
        {
            $( this ).remove();
        } )
        .append(
            $( '<div id="popup_content">' )
            .append( '<img id="popup_content_img" alt="" src=' + a_src + ' class="centerMe" />' )
            .prepend( '<h2 id="popup_content_header">' + a_text + '</h2>' )
            .append( "<p id='popup_content_footer'>Close <strong>X</strong></p>" )
        )
    );
    //Sets the height of the "faded area"
    var $_popupWrap = $( "#popup_wrap" );

    $_popupWrap.height( a_documentHeight );
    $_popupWrap.width( a_documentWidth );

    var $_popupContent = $( "#popup_content" );

    var f_popupImgWidth = $_popupContent.width();
    var f_popupImgHeight = $_popupContent.height();

    var f_position = {
        top: ( ( a_clientHeight - f_popupImgHeight ) * 0.5 ) + a_scrolledTop,
        left: ( ( a_clientWidth - f_popupImgWidth ) * 0.5 )
    };

    //Positions the popup in center of screen
    $_popupContent.css( f_position );
}

//document.Ready() event
$( function ()
{  
    var $_window = $( window ),
        $_document = $( document );

    var f_clientWidth = $_window.width(),
        f_clientHeight = $_window.height();
    
    var f_documentHeight = $_document.height(),
        f_documentWidth = $_document.width();

    var f_scrolledTop = $_window.scrollTop();

    $_window.resize( function ()
    {
        f_clientWidth = $_window.width();
        f_clientHeight = $_window.height();

        f_documentHeight = $_document.height();
        f_documentWidth = $_document.width();
    } );
    
    $_window.scroll( function ()
    {        
        f_scrolledTop = $_window.scrollTop();
    } );    

    //When mouseover/out on a list item
    $( "#model_item_list li" ).hover( function ()
    {
        $( this ).addClass( "model_item_focused" );
    },
    function ()
    {        
        $( this ).removeClass( "model_item_focused" );
    } );

    $_thumbnails = $( ".model_item_thumbnail" ).find( ".model_item_texture_right" ).find( "img" );

    $_thumbnails.click( function ()
    {
        Gorbatras.Admin.ImagePopup( "Thumbnail Image", $( this ).attr( "src" ), f_clientHeight, f_clientWidth, f_documentHeight, f_documentWidth, f_scrolledTop );
    } );
    
    //When user clicks on a texture image
    $_textureImg = $( ".model_item_texture" ).find(".model_item_texture_right").find("img");

    $_textureImg.click( function()
    {      
        var f_textureImg = $( this );
        
        Gorbatras.Admin.ImagePopup( f_textureImg.attr( "alt" ), f_textureImg.attr( "src" ), f_clientHeight, f_clientWidth, f_documentHeight, f_documentWidth, f_scrolledTop );
        
    } );  
} );